package cn.xumaoyang.pet.controller;


import cn.xumaoyang.basic.exception.BusinessException;
import cn.xumaoyang.basic.util.AjaxResult;
import cn.xumaoyang.basic.util.LoginContext;
import cn.xumaoyang.basic.util.PageList;
import cn.xumaoyang.pet.domain.Pet;
import cn.xumaoyang.pet.query.PetQuery;
import cn.xumaoyang.pet.service.IPetService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.OptionalInt;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;


    //宠物管理的分页。
    @PostMapping
    public PageList<Pet> queryPege(@RequestBody PetQuery query){
         return petService.queryPage(query);
    }

    /**
     * 宠物上架，批量上架。
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 宠物上架，批量下架。
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 前端宠物详情页面。
     */
    @GetMapping("/detail/{petId}")
    public Pet detail(@PathVariable("petId") Long petId ){
     return petService.detail(petId);
    }


//    //前端宠物领养的完整实现。
//    @PostMapping("/adopt")
//    public AjaxResult adopt(@RequestBody Map<String,Long> param, HttpServletRequest request){
//        try {
//            petService.adopt(param, LoginContext.getCurrentLoginInfo(request));
//            return new AjaxResult();
//        } catch (BusinessException e) {
//            e.printStackTrace();
//            return new AjaxResult(false,e.getMessage());
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new AjaxResult(false,"操作失败请稍后重试");
//        }
//    }

    //查询单个宠物信息。
    @GetMapping("/{id}")
    public Pet queryById(@PathVariable("id") Long id ){
        return petService.loadById(id);
    }

}
